from loaddata.mgDB import JinCaiDatabase,Wan500Database
import numpy as np
from collections import Counter
# 判断主客差
def zkc(h,a,s):
    c = float(h) - float(a)
    if abs(c) < s:
        return True
    else:
        return False

# 筛选
def select(ds,s):
    selectds = []
    for dd in ds:
        td = {}
        a = dd.get("a")     # 负
        h = dd.get("h")     # 胜
        d = dd.get("d")

        if a and h:
            if zkc(h,a,s):
                # print(h, d, a)
                fixedBonus = dd.get("fixedBonus")
                # 开奖拿出了
                matchResultList=fixedBonus.get("matchResultList")
                for m in matchResultList:
                    if m.get("code") == "HAFU":
                        td["kaijiang"] = m.get("combination")
                        break
                # 全半场数据拿出了
                hafuList = fixedBonus.get("oddsHistory").get("hafuList")
                td["hafu"] = hafuList[-1]
                selectds.append(td)
    return selectds
# 判断中奖
def iszhong(kaijiang,s):
    if kaijiang == s:
        # 命中
        return 1
    else:
        return 0

# 转化为赔序
def peixue(ds):
    rs  = []
    for d in ds:
        hafu =d.get("hafu")
        kaijiang = d.get("kaijiang")
        cips = [('H:H', float(hafu.get("hh")),iszhong(kaijiang,'H:H')),
                ('H:D', float(hafu.get("hd")),iszhong(kaijiang,'H:D')),
                ('H:A', float(hafu.get("ha")),iszhong(kaijiang,'H:A')),
                ('D:H', float(hafu.get("dh")),iszhong(kaijiang,'D:H')),
                ('D:D', float(hafu.get("dd")),iszhong(kaijiang,'D:D')),
                ('D:A', float(hafu.get("da")),iszhong(kaijiang,'D:A')),
                ('A:H', float(hafu.get("ah")),iszhong(kaijiang,'A:H')),
                ('A:D', float(hafu.get("ad")),iszhong(kaijiang,'A:D')),
                ('A:A', float(hafu.get("aa")),iszhong(kaijiang,'A:A'))]
        # 按照赔率，小到大的排序
        tt = sorted(cips, key=lambda e: e[1], reverse=False)
        tt.append(d.get("no"))
        rs.append(tt)
    return rs

# bool->0\1
def boolto01(b):
    if b:
        return 1
    else:
        return 0



'''
统计最大间隔
'''
def tj(dx):
    # 查看一下多少次中奖一次，间隔多少次
    r = []
    a = 0
    for d in dx:
        if d == 0:
            a += 1
        else:
            r.append(a)
            a = 0
    dxres = Counter(dx)
    mzl = round(dxres.get(1)/len(dx),4)


    print("统计：", dxres,"命中率：",mzl)
    print("中奖记录：", dx)
    print("最大间隔：", max(r))
    res = Counter(r)
    # 计算平均间隔
    print("平均间隔：", round(sum(r)/len(r),2))
    print("统计：", res)
    print("间隔多久中：", r)

# 统计
def tongji(ds):
    rs = [0,0,0,0,0,0,0,0,0]
    ts = [0,0,0,0,0,0,0,0,0]
    for dd in ds:
        for i in range(len(dd)):
            d = dd[i]
            # 判断是否中奖
            if d[2]:
                rs[i] += 1
                ts[i] += d[1]

    js = [[boolto01(b[2]) for b in a] for a in ds]

    print("===========命中率统计==========")
    print(rs)
    ra = [round(a/sum(rs),2) for a in rs ]
    print(ra)
    print("===========赔率值统计==========")
    ta = [round(a, 2) for a in ts]
    print(ta)
    pl = []
    for r,t in zip(rs,ts):
        pl.append(round(t/r,2))
    # print(pl)
    print("===========中奖间隔统计==========")
    zjt0 = [a[0] for a in js]
    zjt1 = [a[1] for a in js]
    zjt2 = [a[2] for a in js]
    zjt3 = [a[3] for a in js]
    zjt4 = [a[4] for a in js]
    zjt5 = [a[5] for a in js]
    zjt6 = [a[6] for a in js]
    zjt7 = [a[7] for a in js]
    zjt8 = [a[8] for a in js]

    tj(zjt0)
    tj(zjt1)
    tj(zjt2)
    tj(zjt3)
    tj(zjt4)
    tj(zjt5)
    tj(zjt6)
    tj(zjt7)
    tj(zjt8)

# 根据年份，按照赔率，统计中奖情况
'''
1、先找出符合条件的比赛
2、以天为单位，如果与多场，就选择第一场（最符合的）
3、转化为赔序（几赔）
4、判断开奖情况
5、统计中奖情况（各个赔率的命中率，最大间隔）
'''

def fenxi(s):
    rs = mgDB.get_jincai(None)
    rs = select(rs,s)
    rs = peixue(rs)
    tongji(rs)

# 计算相识度
def hda_ck(aa,bb):

    # 将数组转换为numpy数组
    np_arr1 = np.array(aa)
    np_arr2 = np.array(bb)

    # 计算相似度（这里使用了余弦相似度）
    similarity = np.dot(np_arr1, np_arr2) / (np.linalg.norm(np_arr1) * np.linalg.norm(np_arr2))

    return similarity

# 根据胜平负赔率查询
def getByHDAandhafu(HDA_hafu,x):
    ds = mgDB.get_jincai(None)
    selectds = []
    for ddd in ds:
        td = {}
        a = ddd.get("a")     # 负
        h = ddd.get("h")     # 胜
        d = ddd.get("d")     # 平
        if a and h and d:
            fixedBonus = ddd.get("fixedBonus")
            hafuList = fixedBonus.get("oddsHistory").get("hafuList")
            hafu = hafuList[-1]
            hh = float(hafu.get("hh"))
            hd = float(hafu.get("hd"))
            ha = float(hafu.get("ha"))
            dh = float(hafu.get("dh"))
            dd = float(hafu.get("dd"))
            da = float(hafu.get("da"))
            ah = float(hafu.get("ah"))
            ad = float(hafu.get("ad"))
            aa = float(hafu.get("aa"))
            bb = [float(h),float(d),float(a),hh,hd,ha,dh,dd,da,ah,ad,aa]
            sim=hda_ck(HDA_hafu, bb)
            if sim>x:
                # print(sim)
                # 开奖拿出了
                matchResultList=fixedBonus.get("matchResultList")
                for m in matchResultList:
                    if m.get("code") == "HAFU":
                        td["kaijiang"] = m.get("combination")
                        break
                # 全半场数据拿出了

                td["hafu"] = hafu
                td["no"] = ddd.get("matchTime")
                selectds.append(td)
    tt = sorted(selectds, key=lambda e: e["no"], reverse=True)
    rs = peixue(tt)
    return rs

# 根据胜平负赔率查询
def getByHDA(HDA,x):
    ds = mgDB.get_jincai(None)
    selectds = []
    for ddd in ds:
        td = {}
        a = ddd.get("a")     # 负
        h = ddd.get("h")     # 胜
        d = ddd.get("d")     # 平
        if a and h and d:
            fixedBonus = ddd.get("fixedBonus")
            hafuList = fixedBonus.get("oddsHistory").get("hafuList")
            hafu = hafuList[-1]
            hh = float(hafu.get("hh"))
            hd = float(hafu.get("hd"))
            ha = float(hafu.get("ha"))
            dh = float(hafu.get("dh"))
            dd = float(hafu.get("dd"))
            da = float(hafu.get("da"))
            ah = float(hafu.get("ah"))
            ad = float(hafu.get("ad"))
            aa = float(hafu.get("aa"))
            bb = [hh,hd,ha,dh,dd,da,ah,ad,aa]
            sim=hda_ck(HDA, bb)
            if sim>x:
                # 开奖拿出了
                matchResultList=fixedBonus.get("matchResultList")
                for m in matchResultList:
                    if m.get("code") == "HAFU":
                        td["kaijiang"] = m.get("combination")
                        break
                # 全半场数据拿出了

                td["hafu"] = hafu
                td["no"] = ddd.get("matchTime")
                selectds.append(td)
    tt = sorted(selectds, key=lambda e: e["no"], reverse=True)
    rs = peixue(tt)
    return rs

if __name__ == '__main__':
    s = 0.5
    fenxi(s)